﻿using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Linq;

/// <summary>
/// Creating instance of sounds from code with no effort
/// </summary>
[RequireComponent(typeof(AudioSource))]
public class SoundEffectsHelper : MonoBehaviour
{
	
	/// <summary>
	/// Singleton
	/// </summary>
	public static SoundEffectsHelper Instance;
	
	public SoundObjectScript selectionSound;
	public SoundObjectScript vanishSound;
	public SoundObjectScript jokerSound;
	public AudioSource BGM;
	
	private AudioClip[] song;
	private int currentTrack = 0;
	/*private FileInfo[] soundFiles;
	private List<string> validExtensions = new List<string> { "mp3", "wav" };*/
	
	
	void Awake()
	{
		// Register the singleton
		if ( null == Instance )
			Instance = this;
		else if( Instance != this )
			Destroy(this);
		
		DontDestroyOnLoad(this);
		//AudioListener.volume = 0.05f;
		song = Resources.LoadAll<AudioClip>("") as AudioClip[];
		
		/*/var info = new DirectoryInfo("Assets/Resources");
		var info = new DirectoryInfo("../../../../../../../../Users/PTIM/Musique/ZIK/Ayo/Joyful/");
		soundFiles = info.GetFiles()
			.Where(f => IsValidFileType(f.Name))
				.ToArray();
		
		// and load them
		foreach (var s in soundFiles)
			Debug.Log(s); //StartCoroutine(LoadFile(s.FullName));	*/	
		
	}
	
	/*bool IsValidFileType(string fileName)
	{
		return validExtensions.Contains(fileName.Substring(fileName.LastIndexOf('.') + 1));
	}*/
	
	void Update()
	{
		if( !BGM.isPlaying)
		{
			BGM.clip = song[currentTrack];
			BGM.Play();
			currentTrack++;
			if( currentTrack >= song.Length )
				currentTrack = 0;
		}
	}
	
	public void MakeVanishSound()
	{
		vanishSound.MakeSound();
	}
	
	public void MakeSelectionSound()
	{
		selectionSound.MakeSound();
	}
	
	public void MakeJokerSound()
	{
		jokerSound.MakeSound();
	}
}
